package com.ruoyi.web.controller.system;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysWorkflow;
import com.ruoyi.system.service.ISysWorkflowService;

@Controller
@RequestMapping("/system/workflow")
public class SysWorkflowController extends BaseController
{
	private String prefix = "system/workflow";

	@Autowired
	private ISysWorkflowService workflowService;

	@GetMapping()
	@RequiresPermissions("system:workflow:view")
	public String user()
	{
		return prefix + "/workflow";
	}

	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("system:workflow:list")
	public List<SysWorkflow> list(SysWorkflow workflow)
	{
		startPage();
		return workflowService.selectSysWorkflowList(workflow);
	}

	/**
	 * 新增流程
	 */
	@GetMapping("/add/{workflowId}")
	public String add(@PathVariable("workflowId") Long workflowId, ModelMap mmap)
	{
		mmap.put("workflow", workflowService.selectSysWorkflowByWorkflowId(workflowId));
		return prefix + "/add";
	}

	/**
	 * 新增保存流程
	 */
	@Log(title = "流程管理", businessType = BusinessType.INSERT)
	@RequiresPermissions("system:workflow:add")
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(@Validated SysWorkflow sysWorkflow)
	{
		sysWorkflow.setCreateBy(getLoginName());
		return toAjax(workflowService.insertSysWorkflow(sysWorkflow));
	}

	/**
	 * 修改流程
	 */
	@RequiresPermissions("system:workflow:edit")
	@GetMapping("/edit/{workflowId}")
	public String edit(@PathVariable("workflowId") Long workflowId, ModelMap mmap)
	{
		SysWorkflow workflow = workflowService.selectSysWorkflowByWorkflowId(workflowId);
		mmap.put("sysWorkflow", workflow);
		return prefix + "/edit";
	}

	/**
	 * 修改保存流程
	 */
	@Log(title = "流程管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:workflow:edit")
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(@Validated SysWorkflow sysWorkflow)
	{
		Long workflowId = sysWorkflow.getWorkflowId();
		Assert.notNull(workflowId, "更新失败，缺少更新数据标识");
		sysWorkflow.setUpdateBy(getLoginName());
		return toAjax(workflowService.updateSysWorkflow(sysWorkflow));
	}

}
